﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizMock.Adapter.Runtime;
using System.IO;

namespace BizMock
{    
    public sealed class Submit 
    {
        private static SubmitInfo _submitInfo = new SubmitInfo();

        private static void submitRequest()
        {
            Message msg = createMessage();
            BizMockery.Instance.SubmitRequest(_submitInfo.Uri, msg );
        }

        private static void submitResponse()
        {
            Message msg = createMessage();
            BizMockery.Instance.SubmitResponse(_submitInfo.Uri, msg); 
        }

        private static Message createMessage()
        {
            Message msg = new Message(_submitInfo.File);
            foreach (ContextProperty prop in _submitInfo.Properties)
            {
                msg.PromoteProperty(prop.Name, prop.Namespace, prop.Value, prop.Promote);
            }
            return msg;
        }
    
        public static RequestToInfo Request( params string[] file )
        {
            _submitInfo = new SubmitInfo();
            _submitInfo.File = file;
            return new RequestToInfo();
        }

        public static RequestToInfo Request( BizMockMessage request )
        {
            _submitInfo = new SubmitInfo();
            _submitInfo.File = request.Files;
            _submitInfo.Properties = request.Properties;
            return new RequestToInfo();
        }

        public static ResponseToInfo Response( params string[] file )
        {
            _submitInfo = new SubmitInfo();
            _submitInfo.File = file;
            return new ResponseToInfo();
        }

        public static ResponseToInfo Response( BizMockMessage response )
        {
            _submitInfo = new SubmitInfo();
            _submitInfo.File = response.Files;
            _submitInfo.Properties = response.Properties;
            return new ResponseToInfo();
        }        
       
        
        internal class SubmitInfo
        {
            internal SubmitInfo()
            {
                Properties = new List<ContextProperty>();
            }

            internal string[] File { get; set; }

            internal string Uri { get; set; }

            internal List<ContextProperty> Properties { get; set; }
        }      

        public sealed class RequestToInfo
        {
            public void To( string uri )
            {
                _submitInfo.Uri = uri;
                submitRequest();
            }

            public void To(ReceiveLocationArtifact rcl )
            {
                _submitInfo.Uri = rcl.RclName;
                submitRequest();
            }            
        }

        public sealed class ResponseToInfo
        {
            public void To( string uri )
            {
                _submitInfo.Uri = uri;
                submitResponse();
            }

            public void To( SolicitResponsePortArtifact srp )
            {
                _submitInfo.Uri = srp.PortName;
                submitResponse();
            }

            
           
        }
    }    
}
